package com.dongjiantong.web.boss.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.model.Response;
//import com.dongjiantong.common.redis.Redis;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.AnimalType;
import com.dongjiantong.sys.model.DicData;
import com.dongjiantong.sys.model.DicUnit;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.dongjiantong.sys.service.DicDataService;
import com.dongjiantong.sys.service.DicUnitService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cao
 * @create 2017-11-18 下午1:57
 **/
//@RestController
//@RequestMapping("/api/boss/sys")
@Slf4j
@OpenBean
public class SysController {

    @Reference(version = "1.0.3")
    public AnimalTypeService animalTypeService;

    /*@Autowired
    public Redis redis;*/

    /**
     *动物类型
     * @param level
     * @param pId
     * @return
     */
    //@GetMapping("/animalType")
    @OpenMethod(
            key = "boss.sys.animalType",
            paramNames = {"level","pId"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<AnimalType>> animalType(Integer level,Long pId)
    {

        if(level==1) {
            return Response.ok(animalTypeService.queryOneLevel());
        }
        else if(level==2)
        {
            return Response.ok(animalTypeService.queryTwoLevel(pId));
        }
        else if(level==3)
        {
            return Response.ok(animalTypeService.queryThreeLevel(pId));
        }

        return Response.ok(animalTypeService.queryOneLevel());

    }

    @OpenMethod(
            key = "boss.sys.animalType.tree",
            paramNames = {},
            httpMethods = RequestMethod.GET
    )
    public Response<List<Object>> animalTypeWithTree()
    {

        List<Object> list = treeNode(0L);
        return  Response.ok(list);

    }
    private List<Object> treeNode(long pid)
    {
        List<AnimalType> childTreeNodes = animalTypeService.queryTreeNode(pid);

        List<Object> node = null;

        if(childTreeNodes.size()>0) {
            node = Lists.newArrayList();

            for (AnimalType animalType:childTreeNodes) {
                if (animalType.getLevel()<=2){
                    if(animalType.getParentId()==pid) {
                        Map<String,Object> childArray = Maps.newHashMap();
                        childArray.put("label",animalType.getAnimalName());
                        childArray.put("value",animalType.getAnimalCode());
                           if (animalType.getLevel()==1){
                               childArray.put("children", treeNode(animalType.getId()));
                           }



                        node.add(childArray);

                    }
                }



            }

        }
        return node;
    }

    /*@GetMapping("/redis")
    public String redis()
    {
        redis.set();
        return  redis.get();
    }*/

}
